<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $project_id = $_POST['project_id'];
  $catalogue = $_POST['catalogue'];
  $projectname = addslashes($_POST['name']);
  
  if(trim($_POST['date'])!=''){
    $date = $_POST['date'];
  }else{
    $date = date('Y-m-d');
  }
  
  $area = addslashes($_POST['area']);
  $owner = addslashes($_POST['owner']);
  $year = $_POST['year'];
  $value = $_POST['value'];
  //$value = str_replace(',','',$_POST['value']);
  
  $img_sql = '';
  
  if(isset($_FILES["project_img"]) && is_uploaded_file($_FILES["project_img"]["tmp_name"]) && $_FILES["project_img"]["tmp_name"]!=''){
    
    $handle = new Upload($_FILES['project_img']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 113;
      $handle->image_y = 75;
      $handle->Process('../../images/projects/'.$project_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE projects SET project_img ='".$handle->file_dst_name."' WHERE project_id=$project_id";
        if(!mysql_query($sql))
        {
          @unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
        }
        else
        {
          $handle->image_resize = true;
          $handle->image_ratio_fill = true;
          $handle->file_name_body_pre = 'big_';
          $handle->image_x = 350;
          $handle->image_y = 260;
          $handle->Process('../../images/projects/'.$project_id);
          @unlink('../../images/projects/'.$project_id.'/'.$_POST['old_img']);
          @unlink('../../images/projects/'.$project_id.'/big_'.$_POST['old_img']);
        }
      }
      $handle->clean();
    }
  }
  
  $cert_sql = '';
  $handle = new Upload($_FILES['cert']);
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->Process('../../images/projects/'.$project_id);
        
        if ($handle->processed)
        {
          $sql = 'UPDATE projects SET project_cert ="'.$handle->file_dst_name.'" WHERE project_id='.$project_id;
           if(!mysql_query($sql))
           {
              @unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
           }
           else
           {
            if(file_exists('../../images/projects/'.$project_id.'/'.$_POST['old_cert']) && trim($_POST['old_cert'])!=''){
              unlink('../../images/projects/'.$project_id.'/'.$_POST['old_cert']);
            }
           }
        }
        $cert_sql = ",project_cert='$name'";
        $handle->clean();
    }
  }
  
  $sql = "UPDATE projects SET
    project_cat_id='$catalogue',
    project_date='$date',
    project_title='$projectname',
    project_area='$area',
    project_owner='$owner',
    project_complete_year='$year',
    project_value='$value'
    $img_sql
    WHERE project_id=$project_id";
  $db->execute($sql);
  
  if($_POST['list1SortOrder']!=''){
    $newImgOrderArray = explode('|',$_POST['list1SortOrder']);
    $i = 1;
    $result = array();
    foreach($newImgOrderArray as $value)
    {
        $sql = 'UPDATE project_img SET img_position='.($i++).' WHERE project_img_id='.$value;
        if(mysql_query($sql))
        {
            $result[] = 'success';
        }
        else
        {
            $result[] = 'fail';
        }
    }
  }
  
  echo <<<js
    <script>
      top.window.uploadify_();
    </script>
js;
?>